//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows.Media;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing
{
    public class ColorScheme : IOfficeXmlSerializable
    {
        private ColorBase _accent1;
        private ColorBase _accent2;
        private ColorBase _accent3;
        private ColorBase _accent4;
        private ColorBase _accent5;
        private ColorBase _accent6;
        private ColorBase _dk1;
        private ColorBase _dk2;
        private ColorBase _folHlink;
        private ColorBase _hlink;

        private ColorBase _lt1;
        private ColorBase _lt2;
        private string _name;

        public ColorBase this[ColorSchemeIndex i]
        {
            get
            {
                switch (i)
                {
                    case ColorSchemeIndex.Accent1:
                        return Accent1;
                    case ColorSchemeIndex.Accent2:
                        return Accent2;
                    case ColorSchemeIndex.Accent3:
                        return Accent3;
                    case ColorSchemeIndex.Accent4:
                        return Accent4;
                    case ColorSchemeIndex.Accent5:
                        return Accent5;
                    case ColorSchemeIndex.Accent6:
                        return Accent6;
                    case ColorSchemeIndex.Dark1:
                        return Dark1;
                    case ColorSchemeIndex.Dark2:
                        return Dark2;
                    case ColorSchemeIndex.FollowedHyperlink:
                        return FollowedHyperlink;
                    case ColorSchemeIndex.Hyperlink:
                        return Hyperlink;
                    case ColorSchemeIndex.Light1:
                        return Light1;
                    case ColorSchemeIndex.Light2:
                        return Light2;
                    default:
                        throw new NotSupportedException();
                }
            }
        }

        public ColorBase Dark1
        {
            get { return _dk1; }
            set { _dk1 = value; }
        }

        public ColorBase Light1
        {
            get { return _lt1; }
            set { _lt1 = value; }
        }

        public ColorBase Dark2
        {
            get { return _dk2; }
            set { _dk2 = value; }
        }

        public ColorBase Light2
        {
            get { return _lt2; }
            set { _lt2 = value; }
        }

        public ColorBase Accent1
        {
            get { return _accent1; }
            set { _accent1 = value; }
        }

        public ColorBase Accent2
        {
            get { return _accent2; }
            set { _accent2 = value; }
        }

        public ColorBase Accent3
        {
            get { return _accent3; }
            set { _accent3 = value; }
        }

        public ColorBase Accent4
        {
            get { return _accent4; }
            set { _accent4 = value; }
        }

        public ColorBase Accent5
        {
            get { return _accent5; }
            set { _accent5 = value; }
        }

        public ColorBase Accent6
        {
            get { return _accent6; }
            set { _accent6 = value; }
        }

        public ColorBase Hyperlink
        {
            get { return _hlink; }
            set { _hlink = value; }
        }

        public ColorBase FollowedHyperlink
        {
            get { return _folHlink; }
            set { _folHlink = value; }
        }


        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public ColorBase Background1
        {
            get { return Light1; }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("clrScheme"))
            {
                Name = c.GetString("name");
                Dark1 = ColorBase.ReadColorAs("dk1", c);
                Light1 = ColorBase.ReadColorAs("lt1", c);
                Dark2 = ColorBase.ReadColorAs("dk2", c);
                Light2 = ColorBase.ReadColorAs("lt2", c);
                Accent1 = ColorBase.ReadColorAs("accent1", c);
                Accent2 = ColorBase.ReadColorAs("accent2", c);
                Accent3 = ColorBase.ReadColorAs("accent3", c);
                Accent4 = ColorBase.ReadColorAs("accent4", c);
                Accent5 = ColorBase.ReadColorAs("accent5", c);
                Accent6 = ColorBase.ReadColorAs("accent6", c);
                _hlink = ColorBase.ReadColorAs("hlink", c);
                FollowedHyperlink = ColorBase.ReadColorAs("folHlink", c);
                c.WarnIf("ext");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("clrScheme"))
            {
                c.SetString("name", Name);
                WriteColorAs(Dark1, "dk1", c);
                WriteColorAs(Light1, "lt1", c);
                WriteColorAs(Dark2, "dk2", c);
                WriteColorAs(Light2, "lt2", c);
                WriteColorAs(Accent1, "accent1", c);
                WriteColorAs(Accent2, "accent2", c);
                WriteColorAs(Accent3, "accent3", c);
                WriteColorAs(Accent4, "accent4", c);
                WriteColorAs(Accent5, "accent5", c);
                WriteColorAs(Accent6, "accent6", c);
                WriteColorAs(_hlink, "hlink", c);
                WriteColorAs(FollowedHyperlink, "folHlink", c);
                c.WarnIf("ext");
            }
        }

        #endregion

        private void WriteColorAs(ColorBase d, string name, WriteContext context)
        {
            using (WriteContext c = context.Write(name))
            {
                c.WriteElement(d);
            }
        }
    }
}